
var  consts  = require('../consts');

var order= require('../db/order');
var fs =require('fs');
//var cache=require('../redis/usercache');

var oauth2= require('../network/oauth2');
var member= require('../db/member');
var db= require('../db/dbconnection');

function processRefundDeclare(request,response,callback){

	  fs.readFile('data/refund-declare.txt', "utf8", function (err, data) {
		 
		  if (err){
			  console.log(err.message);
			  response.errorCode=consts.Error.ERROR_GENERAL;
			  response.message=consts.Message.UNKNOWN;
			  if(callback){
			      callback(response);
			  }			  
		  }else{
			  response.errorCode = consts.Error.NONE_ERROR;
			  response.message= consts.Message.OK_MESSAGE;
			  var obj={};
			  obj.type=	consts.Resource.HTML;
			  obj.text=data;
			  response.data = JSON.stringify(obj);
			
			  if(callback){
				  callback(response);
			  } 
		  }
		  
	  });

}


function processOrderEarningStatisticsReport(request,response,callback){

	var data =request.data;
	
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	var param = null;
	
	try {
		param=JSON.parse(data);
	} catch (e) {
	}
	var type = 1;
//	value = new Date().get
	if(param !==null && typeof(param.type) !== 'undefined' &&  param.type !== null )  {
		type = param.type;
	} 
	
	if(type !==1 && type !==7 && type!== 30 &&type !==12  && type !== 4) {
		type =1;
	}
	var datetime = new Date();
	
	var storeID = -1;
	if(param !==null && typeof(param.datetime) !== 'undefined' &&  param.datetime !== null &&  param.datetime !== 0 )  {
		datetime.setTime(param.datetime);
	}
	
	if(param !==null && typeof(param.storeID) !== 'undefined' &&  param.storeID !== null &&  param.storeID !== 0 )  {
		storeID =  param.storeID;
	}
	

	order.getEarningsStatisticsReport(request.token,storeID, type,datetime,function(err,wrap){
		if(err){
			response.errorCode = err.number;
			response.message= consts.message;
			response.data = JSON.stringify(wrap);
			if(callback){
				callback(response);
			}
		}else {
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(wrap);
			if(callback){
				callback(response);
			}
		}
		
	});

	
	return;

}



function processOrderPayStatisticsReport(request,response,callback){

	var data =request.data;
	
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	var param = null;
	
	try {
		param=JSON.parse(data);
	} catch (e) {
	}
	var type = 1;
//	value = new Date().get
	if(param !==null && typeof(param.type) !== 'undefined' &&  param.type !== null )  {
		type = param.type;
	} 
	
	if(type !==1 && type !==7 && type!== 30 &&type !==12  && type !== 4) {
		type =1;
	}
	var datetime = new Date();
	
	var storeID = -1;
	if(param !==null && typeof(param.datetime) !== 'undefined' &&  param.datetime !== null &&  param.datetime !== 0 )  {
		datetime.setTime(param.datetime);
	}
	
	if(param !==null && typeof(param.storeID) !== 'undefined' &&  param.storeID !== null &&  param.storeID !== 0 )  {
		storeID =  param.storeID;
	}
	

	order.getPayStatisticsReport(request.token,storeID, type,datetime,function(err,wrap){
		if(err){
			response.errorCode = err.number;
			response.message= consts.message;
			response.data = JSON.stringify(wrap);
			if(callback){
				callback(response);
			}
		}else {
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(wrap);
			if(callback){
				callback(response);
			}
		}
		
	});

	
	return;

}


function processOrderEarningStatistics(request,response,callback){

	var data =request.data;
	
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	order.getEarningsStatistics(request.token,function(err,wrap){
		if(err){
			response.errorCode = err.number;
			response.message= consts.message;
			response.data = JSON.stringify(wrap);
			if(callback){
				callback(response);
			}
		}else {
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(wrap);
			if(callback){
				callback(response);
			}
		}
	
	});
	
	return;

}




function processGiftList(request,response,callback){

	var data =request.data;
	
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}


	var orderNO = null;

	if(data!==null && typeof(data)!=='undefined'){
	
		try {
		    param = JSON.parse(data);

		} catch (e) {
			
		
		}
	}
	
	
	if(param!==null){
		if(param.orderNO!==null && typeof(param.orderNO)!=='undefined'){
			orderNO=param.orderNO;
		}
		
	}
	
	if(!orderNO){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
	}
	
	order.getGiftList(request.token,orderNO, function(err,wrap){
		if(err){
			
			response.errorCode=err.number;
			response.message=err.message;
			if(callback){
				callback(response);
			}
			return;
		}
		
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(wrap);
		if(callback){
			callback(response);
		}
		 
	});

	return;

}



function processReserveList(request,response,callback){

	var data =request.data;
	
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	var pageNO=0;
	var pageSize=20;
	var todaySum=0;
	var totalCount=0;
	var from = null;
	var to = null;
	var reserveType =-1;
	var search = null;
	
	var param = null;

	if(data!==null && typeof(data)!=='undefined'){
	
		try {
		    param = JSON.parse(data);

		} catch (e) {
			
		
		}
	}
	
	
	if(param!==null){
		if(param.pageNO!==null && typeof(param.pageNO)!=='undefined'){
			pageNO=param.pageNO;
		}
		
		if(param.pageSize!==null && typeof(param.pageSize)!=='undefined'){
			pageSize=param.pageSize;
		}
		
		if(param.todaySum!==null && typeof(param.todaySum)!=='undefined'){
				
			todaySum=param.todaySum;
		
		}
		
		if(param.totalCount!==null && typeof(param.totalCount)!=='undefined'){
		
			totalCount=param.totalCount;
	
		}
		if(param.fromTime!==null && typeof(param.fromTime)!=='undefined'){

			from=param.fromTime;
		
		}
		if(param.toTime!==null && typeof(param.toTime)!=='undefined'){
			to=param.toTime;
		
		}
		
	   if(param.reserveType!==null && typeof(param.reserveType)!=='undefined'){
			reserveType=param.reserveType;
			console.log('reserveType:::' + reserveType);
		
		}
	   
	   if(param.search!==null && typeof(param.search)!=='undefined'){
			search=param.search;
//			console.log('reserveType:::' + reserveType);
		
		}
		
		
	}
	
	
	order.getReserveList(request.token,reserveType,pageNO,pageSize,todaySum, totalCount, from,to,search, function(err,wrap){
		if(err){
			
			response.errorCode=err.number;
			response.message=err.message;
			if(callback){
				callback(response);
			}
			return;
		}
		
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(wrap);
		if(callback){
			callback(response);
		}
		 
	});

	return;

}








function processPayOrderList(request,response,callback){

	var data =request.data;
	
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	var pageNO=0;
	var pageSize=20;
	var todaySum=0;
	var totalCount=0;
	var from = null;
	var to = null;
	
	
	var param = null;

	if(data!==null && typeof(data)!=='undefined'){
	
		try {
		    param = JSON.parse(data);

		} catch (e) {
			
		
		}

	}
	
	
	if(param!==null){
		if(param.pageNO!==null && typeof(param.pageNO)!=='undefined'){
			pageNO=param.pageNO;
		}
		
		if(param.pageSize!==null && typeof(param.pageSize)!=='undefined'){
			pageSize=param.pageSize;
		}
		
		if(param.todaySum!==null && typeof(param.todaySum)!=='undefined'){
		
			todaySum=param.todaySum;
		
		}
		
		if(param.totalCount!==null && typeof(param.totalCount)!=='undefined'){
			totalCount=param.totalCount;
		
		}
		if(param.fromTime!==null && typeof(param.fromTime)!=='undefined'){
			from=param.fromTime;
		}
		if(param.toTime!==null && typeof(param.toTime)!=='undefined'){
			to=param.toTime;
		}
		
		
	}
	
	if(pageSize < 0){
		pageSize = 1000000;
	}
	
	
	order.getPayOrderList(request.token,pageNO,pageSize,todaySum, totalCount, from,to,  function(err,wrap){
		if(err){
			
			response.errorCode=err.number;
			response.message=err.message;
			if(callback){
				callback(response);
			}
			return;
		}
		
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(wrap);
		if(callback){
			callback(response);
		}
		 
	});

	return;

}


function processUserOrderList(request,response,callback){

	var data =request.data;
	
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	
	var pageNO =0;
	var pageSize=12;
    var totalCount=0;
	var deskNO = null;
	var orderNO = null;
	var payStatus=-1;
	var serviceStatus = -1;
	var search =  null;
	var detail =0;
	var deskOnly=0;
	
	var param = null;

	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
	
	try {
	    param = JSON.parse(data);

	} catch (e) {
		
	
	}

	if(param!==null){
		if(param.pageNO!==null && typeof(param.pageNO)!=='undefined'){
			pageNO=param.pageNO;
		}
		
		if(param.pageSize!==null && typeof(param.pageSize)!=='undefined'){
			pageSize=param.pageSize;
		}
		
		
		if(param.totalCount!==null && typeof(param.totalCount)!=='undefined'){
			totalCount=param.totalCount;
		
		}
		
		
		if(param.search!==null && typeof(param.search)!=='undefined'){
			search=param.search;
		}
		
		if(param.deskOnly){
			deskOnly=param.deskOnly;
		}
		
		
		if(param.deskNO!==null && typeof(param.deskNO)!=='undefined'){
			deskNO=param.deskNO;
		}
		
		
		if(param.orderNO!==null && typeof(param.orderNO)!=='undefined'){
			orderNO=param.orderNO;
		}
		
		if(param.payStatus!==null && typeof(param.payStatus)!=='undefined'){
			payStatus=param.payStatus;
		}
		
	   if(param.serviceStatus!==null && typeof(param.serviceStatus)!=='undefined'){
			serviceStatus=param.serviceStatus;
		}
	    
	   if(param.detail && param.detail==1){
	    	detail =param.detail;
	    }
		
		
	}
	
	//token,pageNO,len, totalCount, , 
	
	order.getUserOrderList(request.token,pageNO, pageSize,totalCount, deskNO,orderNO,payStatus,serviceStatus,search, detail, deskOnly, function(err,wrap){
		if(err){
			
			response.errorCode=err.number;
			response.message=err.message;
			if(callback){
				callback(response);
			}
			return;
		}
		
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(wrap);
		if(callback){
			callback(response);
		}
		 
	});

	return;

}




function processUserOrderPayFlow(request,response,callback){

	var data =request.data;
	
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	var param = null;

	if(data!==null && typeof(data)!=='undefined'){
		console.log(data);
	}
	
	try {
	    param = JSON.parse(data);

	} catch (e) {
		
	
	}
	var cataID = -1;
	var fromTime = new Date().getTime() -1000*60*60*24*30;
	var toTime = new Date().getTime();

	if(param!==null){
	
		
		if(param.fromTime!==null && typeof(param.fromTime)!=='undefined'){
			fromTime=param.fromTime;
		}
		
		if(param.toTime!==null && typeof(param.toTime)!=='undefined'){
			toTime=param.toTime;
		}
		
		if(param.cataID!==null && typeof(param.cataID)!=='undefined'){
			cataID=param.cataID;
		
		}
		
	}
	
	order.getOrderPayFlow(request.token,fromTime, toTime, function(err,wrap){
		if(err){
			
			response.errorCode=err.number;
			response.message=err.message;
			if(callback){
				callback(response);
			}
			return;
		}
		
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(wrap);
		if(callback){
			callback(response);
		}
		 
	});

	return;

}




function processUserOrderS500(request,response,callback){

	var data =request.data;
	
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	var param = null;

	if(data!==null && typeof(data)!=='undefined'){
		console.log(data);
	}
	
	try {
	    param = JSON.parse(data);

	} catch (e) {
		
	
	}
	var cataID = -1;
	var fromTime = new Date().getTime() -1000*60*60*24*30;
	var toTime = new Date().getTime();

	if(param!==null){
	
		
		if(param.fromTime!==null && typeof(param.fromTime)!=='undefined'){
			fromTime=param.fromTime;
		}
		
		if(param.toTime!==null && typeof(param.toTime)!=='undefined'){
			toTime=param.toTime;
		}
		
		if(param.cataID!==null && typeof(param.cataID)!=='undefined'){
			cataID=param.cataID;
		
		}
		
	}
	
	order.getList500(request.token,fromTime, toTime, function(err,wrap){
		if(err){
			
			response.errorCode=err.number;
			response.message=err.message;
			if(callback){
				callback(response);
			}
			return;
		}
		
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(wrap);
		if(callback){
			callback(response);
		}
		 
	});

	return;

}



function processUserPayStatistics(request,response,callback){

	var data =request.data;
	
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	var param = null;

	if(data!==null && typeof(data)!=='undefined'){
		console.log(data);
	}
	
	try {
	    param = JSON.parse(data);

	} catch (e) {
		
	
	}
	
	var fromTime = new Date().getTime() -1000*60*60*24*30;
	var toTime = new Date().getTime();
	var cataID=-1;
	

	if(param!==null){
	
		
		if(param.fromTime!==null && typeof(param.fromTime)!=='undefined'){
			fromTime=param.fromTime;
		}
		
		if(param.toTime!==null && typeof(param.toTime)!=='undefined'){
			toTime=param.toTime;
		}
		
		if(param.cataID!==null && typeof(param.cataID)!=='undefined'){
			cataID=param.cataID;
		
		}
		
	}
	
	order.getUserPayStatistics(request.token,fromTime, toTime, function(err,wrap){
		if(err){
			
			response.errorCode=err.number;
			response.message=err.message;
			if(callback){
				callback(response);
			}
			return;
		}
		
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(wrap);
		if(callback){
			callback(response);
		}
		 
	});

	return;

}


function processUserOrderStatistics(request,response,callback){

	var data =request.data;
	
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	

	var param = null;

	if(data!==null && typeof(data)!=='undefined'){
		console.log(data);
	}
	
	try {
	    param = JSON.parse(data);

	} catch (e) {
	}
	
	var fromTime = new Date().getTime() -1000*60*60*24*30;
	var toTime = new Date().getTime();
	var cataID=-1;
	

	if(param!==null){
	
		
		if(param.fromTime!==null && typeof(param.fromTime)!=='undefined'){
			fromTime=param.fromTime;
		}
		
		if(param.toTime!==null && typeof(param.toTime)!=='undefined'){
			toTime=param.toTime;
		}
		
		if(param.cataID!==null && typeof(param.cataID)!=='undefined'){				
			cataID=param.cataID;
		}
		
	}
	
	order.getUserOrderStatistics(request.token, cataID,fromTime, toTime, function(err,wrap){
		if(err){
			
			response.errorCode=err.number;
			response.message=err.message;
			if(callback){
				callback(response);
			}
			return;
		}
		
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(wrap);
		if(callback){
			callback(response);
		}
		 
	});

	return;

}


function processUserOrderStatistics1(request,response,callback){

	var data =request.data;
	
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	

	var param = null;

	if(data!==null && typeof(data)!=='undefined'){
		console.log(data);
	}
	
	try {
	    param = JSON.parse(data);

	} catch (e) {
	}
	
	var fromTime = new Date().getTime() -1000*60*60*24*30;
	var toTime = new Date().getTime();
	var cataID=-1;
	

	if(param!==null){
	
		
		if(param.fromTime!==null && typeof(param.fromTime)!=='undefined'){
			fromTime=param.fromTime;
		}
		
		if(param.toTime!==null && typeof(param.toTime)!=='undefined'){
			toTime=param.toTime;
		}
		
		if(param.cataID!==null && typeof(param.cataID)!=='undefined'){				
			cataID=param.cataID;
		}
		
	}
	
	order.getUserOrderStatistics1(request.token, cataID,fromTime, toTime, function(err,wrap){
		if(err){
			
			response.errorCode=err.number;
			response.message=err.message;
			if(callback){
				callback(response);
			}
			return;
		}
		
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(wrap);
		if(callback){
			callback(response);
		}
		 
	});

	return;

}





function processOrderSummary(request,response,callback){

	var data =request.data;

	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	var id=null;
	var payNO=null;
	var param = null;

	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
	
	try {
	    param = JSON.parse(data);

	} catch (e) {
		
	
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	
	}
	
	if(param.ID!==null && typeof(param.ID)!=='undefined'){
		id=param.ID;
	}
	
	if(param.payNO!==null && typeof(param.payNO)!=='undefined'){
		payNO=param.payNO;
	}
	
	if(id===null && payNO===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':ID or payNO';
		if(callback){
			callback(response);
		}
		return;		
	}
	
	order.getPaySummary(id,payNO,request.token,function(err,obj){
		if(err){
			 response.errorCode=err.number;
				
			 response.message=err.message;
			if(callback){
				callback(response);
			}
			return;
		}
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(obj);
		if(callback){
			callback(response);
		 }
		
		
	});
	
	

}


function processPaySummary(request,response,callback){

	var data =request.data;

	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	var id=null;
	var payNO=null;
	var param = null;

	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
	
	try {
	    param = JSON.parse(data);

	} catch (e) {
		
	
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	
	}
	
	if(param.ID!==null && typeof(param.ID)!=='undefined'){
		id=param.ID;
	}
	
	if(param.payNO!==null && typeof(param.payNO)!=='undefined'){
		payNO=param.payNO;
	}
	
	if(id===null && payNO===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':ID or payNO';
		if(callback){
			callback(response);
		}
		return;		
	}
	
	order.getPaySummary(id,payNO,request.token,function(err,obj){
		if(err){
			 response.errorCode=err.number;
				
			 response.message=err.message;
			if(callback){
				callback(response);
			}
			return;
		}
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(obj);
		if(callback){
			callback(response);
		 }
		
		
	});
	
	

}




function  processTransferinList(request,response,callback){
	var data =request.data;
	  
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	var items=[];
	var result= {};
//	var before = 30;
//	for(var i=0;i<20;i++){
//		var item = {};
//		item.gainedDate=Date.now()-3600*(before+i)*24;
//		item.sum=30.0*i;
//		item.status=20;// 已划款
//		items.push(item);
//	}
	
    result.count=items.length;	
    if(result.count >0){
    	result.items=items;

    }
	response.errorCode = consts.Error.NONE_ERROR;
	response.message= consts.Message.OK_MESSAGE;
	response.data = JSON.stringify(result);
	if(callback){
		callback(response);
	} 
	
	return;
	
//	cache.getByToken(request.token,function(tk,entity){
//		if(tk==null | typeof(tk)=='undefined'){
//			response.errorCode=consts.Error.NEED_LOGIN;
//			response.message=consts.Message.NEED_LOGIN;
//			if(callback){
//				callback(response);
//			}
//			return;
//		}
//		
//		 order.getRefundable(param.ID,function(err,result){
//			 if(err){
//				 response.errorCode=consts.Error.ERROR_PARAM;
//				 response.message=consts.Message.ERROR_PARAM;
//				if(callback){
//					callback(response);
//				}
//				 
//			 }else{
//				 response.errorCode = consts.Error.NONE_ERROR;
//				 response.message= consts.Message.OK_MESSAGE;
//				 response.data = JSON.stringify(result);
//				 if(callback){
//					callback(response);
//				} 
//			 }
//		 });
//	
//	});

}



function processCancel(request,response,callback){

	var data =request.data;

	if(request.token===null || typeof(request.token)==='undefined'){

		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;

		if(callback){
			callback(response);
		}
		return;
	}

	var param = null;

	if(data===null || typeof(data)==='undefined'){

		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;

		if(callback){
			callback(response);
		}

		return false;

	}else {
		console.log(data);
	}

	try {

	   param = JSON.parse(data);
	
	} catch (e) {
      console.log(e.message);
	}
	

	if(param===null){

		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;

		if(callback){

			callback(response);

		}

		return false;

	}

	if(param.orderNO===null || typeof(param.orderNO)==='undefined'){

		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':orderNO';

		if(callback){
			callback(response);
		}
		return;		
	}
		
	order.cancelOrder(request.token,param.orderNO,function(err){

		if(err){

			response.errorCode=consts.Error.ERROR_PARAM;
			response.message=consts.Message.ERROR_PARAM;

			if(callback){

				callback(response);
			}

		}else{

			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			if(callback){
				callback(response);
			} 
		}

	});

	return;
	

}



function processCounterPay(request,response,callback){

	var data =request.data;

	var force = false;
	if(request.token===null || typeof(request.token)==='undefined'){

		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;

		if(callback){
			callback(response);
		}
		return;
	}

	var param = null;

	if(data===null || typeof(data)==='undefined'){

		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;

		if(callback){
			callback(response);
		}

		return false;

	}else {
		console.log(data);
	}

	try {

	   param = JSON.parse(data);
	
	} catch (e) {
      console.log(e.message);
	}
	

	if(param===null){

		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;

		if(callback){

			callback(response);

		}

		return false;

	}

	if(param.orderNO===null || typeof(param.orderNO)==='undefined'){

		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':orderNO';

		if(callback){
			callback(response);
		}
		return;		
	}
	
	if(param.force!==null && typeof(param.force)!=='undefined'){
		force = param.force;
	}
		
	order.counterPay(request.token,param.orderNO,force, function(err){

		if(err){

			response.errorCode=consts.Error.ERROR_PARAM;
			response.message=consts.Message.ERROR_PARAM;

			if(callback){

				callback(response);
			}

		}else{

			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			if(callback){
				callback(response);
			} 
		}

	});

	return;
	

}




function processStatusQuery(request,response,callback){

	var data =request.data;

	if(request.token===null || typeof(request.token)==='undefined'){

		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;

		if(callback){
			callback(response);
		}
		return;
	}

	var param = null;

	if(data===null || typeof(data)==='undefined'){

		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;

		if(callback){
			callback(response);
		}

		return false;

	}else {
		console.log(data);
	}

	try {

	   param = JSON.parse(data);
	
	} catch (e) {
      console.log(e.message);
	}
	

	if(param===null){

		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;

		if(callback){

			callback(response);

		}

		return false;

	}

	if(param.payNO===null || typeof(param.payNO)==='undefined'){

		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':payNO';

		if(callback){
			callback(response);
		}
		return;		
	}
	
	var vip = 0;
	
	if(param.vip ===1){
		vip = param.vip;
	}
	
		
	order.getSimpleStatus(request.token,param.payNO,vip,function(err,result){

		if(err){

			response.errorCode=consts.Error.ERROR_PARAM;
			response.message=consts.Message.ERROR_PARAM;

			if(callback){

				callback(response);
			}

		}else{

			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(result);

			if(callback){
				callback(response);
			} 

		}

	});

	return;
	

}



function processOrderAppoint(request,response,callback){

	var data =request.data;

	if(request.muuid===null || typeof(request.muuid)==='undefined'){

		response.errorCode=consts.Error.NEED_MERCHANT;
		response.message=consts.Message.NEED_MERCHANT;

		if(callback){
			callback(response);
		}
		return;
	}

	var param = null;

	if(data===null || typeof(data)==='undefined'){

		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;

		if(callback){
			callback(response);
		}

		return false;

	}else {
		console.log(data);
	}

	try {

	   param = JSON.parse(data);
	
	} catch (e) {
      console.log(e.message);
	}
	

	if(param===null){

		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;

		if(callback){

			callback(response);

		}

		return false;

	}
	param.token=request.token;

	/*
	 * 							param.sex= (val.sex==1)?1:((val.sex==2)?0:-1);
	 * 
	 * Table: reserve
Columns:
id	int(11) AI PK
username	varchar(255)
sex	int(11)
phone	varchar(255)
eat_time	date
people_count	int(11)
remark	varchar(255)
oid	int(11)
type	int(11)
uid	int(11)
create_time	datetime
cp_type	int(11)
	 */
	
	if(param.reserve===null || typeof(param.reserve)==='undefined'){

		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':reserve';

		if(callback){
			callback(response);
		}
		return;		
	}
	
	if(param.money===null || typeof(param.money)==='undefined' || param.money<0.01){
		param.money =null;
	}
	
	param.reserve.reserveType = 0; //0 is appoint

	param.orderNO =  'd'+new Date().getTime()+'d';
	 
//	if(param.reserve.totalNum==null || typeof(param.reserve.totalNum)=='undefined'){
//		param.reserve.totalNum=0;
//	}

	order.insert(request.muuid,param,function(err,result){

		if(err){

			if(typeof(err.number)!== 'undefined' && err.number !== null) {
				response.errorCode = err.number;
				response.message = err.message;
				if(result!==null){
				   response.data = JSON.stringify(result);
				}

				if(callback){
					callback(response);
				}
				return;
				
			}
			response.errorCode=consts.Error.ERROR_PARAM;
			response.message=consts.Message.ERROR_PARAM;

			if(callback){

				callback(response);
			}

		}else{

			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(result);

			if(callback){
				callback(response);
			} 

		}

	});

	return;
	

}




function processOrderUpdatePrintStatus(request,response,callback){

	var data =request.data;

	if(request.muuid===null || typeof(request.muuid)==='undefined'){

		response.errorCode=consts.Error.NEED_MERCHANT;
		response.message=consts.Message.NEED_MERCHANT;

		if(callback){
			callback(response);
		}
		return;
	}

	var param = null;

	if(data===null || typeof(data)==='undefined'){

		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;

		if(callback){
			callback(response);
		}

		return false;

	}else {
		console.log(data);
	}

	try {

	   param = JSON.parse(data);
	
	} catch (e) {
      console.log(e.message);
	}
	
	if(param===null){

		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;

		if(callback){

			callback(response);

		}

		return false;

	}
	
	
	if(param.ID===null || typeof(param.ID)==='undefined' || param.ID===0  ){

		param.ID = null;
	}
	
	
	if(param.orderNO===null || typeof(param.orderNO)==='undefined' ){

		param.orderNO = null;
			
	}
	

	if(param.orderNO ===null && param.ID === null ){
		
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':ID or orderNO';
		if(callback){
			callback(response);
		}
		return;
		
	}
	
	 order.updatePrintStatus(request.muuid,param,function(err,result){

		if(err){

			response.errorCode=consts.Error.ERROR_PARAM;
			response.message=consts.Message.ERROR_PARAM;

			if(callback){
				callback(response);
			}

		}else{

			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(result);

			if(callback){
				callback(response);
			} 

		}

	});

	return;

}


function processUserOrderS501(request,response,callback){
    
    var data =request.data;
    
    if(request.muuid===null || typeof(request.muuid)==='undefined'){
        
        response.errorCode=consts.Error.NEED_MERCHANT;
        response.message=consts.Message.NEED_MERCHANT;
        
        if(callback){
            callback(response);
        }
        return;
    }
    
    var param = null;
    
    if(data===null || typeof(data)==='undefined'){
        
        response.errorCode=consts.Error.ERROR_PARAM;
        response.message=consts.Message.ERROR_PARAM;
        
        if(callback){
            callback(response);
        }
        
        return false;
        
    }else {
        console.log(data);
    }
    
    try {
        
        param = JSON.parse(data);
        
    } catch (e) {
        console.log(e.message);
    }
    if(param===null){
        
        response.errorCode=consts.Error.ERROR_PARAM;
        response.message=consts.Message.ERROR_PARAM;
        
        if(callback){
            
            callback(response);
            
        }
        
        return false;
        
    }
    
    if(param.toDeskNO===null || typeof(param.toDeskNO)==='undefined'){
        param.toDeskNO =null;
    }
    
    if(param.toTime===null || typeof(param.toTime)==='undefined' ||
       param.toTime ===0 ){
        param.toTime =0;
    }
    
    if( param.toDeskNO===null && param.toTime ===0){
        
        response.errorCode=consts.Error.ERROR_PARAM;
        response.message=consts.Message.ERROR_PARAM+':桌台或者时间';
        
        if(callback){
            callback(response);
        }
        return;
    }
    
    
    if(param.ID===null || typeof(param.ID)==='undefined' || param.ID===0  ){
        
        param.ID = null;
    }
    
    
    if(param.orderNO===null || typeof(param.orderNO)==='undefined' ){
        
        param.orderNO = null;
        
    }
    
    
    if(param.orderNO ===null && param.ID === null ){
        
        response.errorCode=consts.Error.ERROR_PARAM;
        response.message=consts.Message.ERROR_PARAM+':ID or orderNO';
        if(callback){
            callback(response);
        }
        return;
        
    }
    
    order.updateS501(request.muuid,param,function(err,result){
                     
                     if(err){
                     
                     response.errorCode=consts.Error.ERROR_PARAM;
                     response.message=consts.Message.ERROR_PARAM;
                     
                     if(callback){
                     callback(response);
                     }
                     
                     }else{
                     
                     response.errorCode = consts.Error.NONE_ERROR;
                     response.message= consts.Message.OK_MESSAGE;
                     response.data = JSON.stringify(result);
                     
                     if(callback){
                     callback(response);
                     }
                     
                     }
                     
                     });
    
    return;
    
}





function processOrderMerge(request,response,callback){

	var data =request.data;

	if(request.muuid===null || typeof(request.muuid)==='undefined'){

		response.errorCode=consts.Error.NEED_MERCHANT;
		response.message=consts.Message.NEED_MERCHANT;

		if(callback){
			callback(response);
		}
		return;
	}

	var param = null;

	if(!data){

		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;

		if(callback){
			callback(response);
		}

		return false;

	}else {
		console.log(data);
	}

	try {

	   param = JSON.parse(data);
	
	} catch (e) {
      console.log(e.message);
	}
	if(!param){

		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;

		if(callback){

			callback(response);

		}

		return false;

	}
	
	
	if(!param.ID ){

		param.ID = null;
	}
	
	
	if(!param.orderNO ){

		param.orderNO = null;
			
	}
	
	if(!param.IDs ){

		param.IDs = null;
			
	}
	
	if( !param.ID && !param.orderNO ){
		
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':ID or orderNO';
		if(callback){
			callback(response);
		}
		return;
		
	}
	if(!param.IDs){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':IDs';
		if(callback){
			callback(response);
		}
		return;
		
	}else{
		 order.merge(request.muuid,param,function(err,result){

				if(err){

					response.errorCode=consts.Error.ERROR_PARAM;
					response.message=consts.Message.ERROR_PARAM;

					if(callback){
						callback(response);
					}

				}else{

					response.errorCode = consts.Error.NONE_ERROR;
					response.message= consts.Message.OK_MESSAGE;
					response.data = JSON.stringify(result);

					if(callback){
						callback(response);
					} 

				}

			});
		
	}
	
	

	return;

}






function processOrderUpdateDeskOrPush(request,response,callback){

	var data =request.data;

	if(request.muuid===null || typeof(request.muuid)==='undefined'){

		response.errorCode=consts.Error.NEED_MERCHANT;
		response.message=consts.Message.NEED_MERCHANT;

		if(callback){
			callback(response);
		}
		return;
	}

	var param = null;

	if(data===null || typeof(data)==='undefined'){

		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;

		if(callback){
			callback(response);
		}

		return false;

	}else {
		console.log(data);
	}

	try {

	   param = JSON.parse(data);
	
	} catch (e) {
      console.log(e.message);
	}
	if(param===null){

		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;

		if(callback){

			callback(response);

		}

		return false;

	}
	
	if(param.toDeskNO===null || typeof(param.toDeskNO)==='undefined'){
		param.toDeskNO =null;
	}
	
	
	
	
	if( param.toDeskNO===null && !param.push){

		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':toDeskNO or push';

		if(callback){
			callback(response);
		}
		return;		
	}
	
	
	if(param.ID===null || typeof(param.ID)==='undefined' || param.ID===0  ){

		param.ID = null;
	}
	
	
	if(param.orderNO===null || typeof(param.orderNO)==='undefined' ){

		param.orderNO = null;
			
	}
	

	
	if(param.reserve===null || typeof(param.reserve)==='undefined' ){

		param.deskStatus = 1;
			
	}else if(param.reserve ===1 ){
		param.deskStatus = 2;

	}else {
		param.deskStatus = 1;

	}
	
	
	if(param.orderNO ===null && param.ID === null ){
		
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':ID or orderNO';
		if(callback){
			callback(response);
		}
		return;
		
	}
	if(param.push ===1){
		 order.push(request.muuid,param,function(err,result){

				if(err){

					response.errorCode=consts.Error.ERROR_PARAM;
					response.message=consts.Message.ERROR_PARAM;

					if(callback){
						callback(response);
					}

				}else{

					response.errorCode = consts.Error.NONE_ERROR;
					response.message= consts.Message.OK_MESSAGE;
					response.data = JSON.stringify(result);

					if(callback){
						callback(response);
					} 

				}

			});
		
		
	}else{
		 order.updateDesk(request.muuid,param,function(err,result){

				if(err){

					response.errorCode=consts.Error.ERROR_PARAM;
					response.message=consts.Message.ERROR_PARAM;

					if(callback){
						callback(response);
					}

				}else{

					response.errorCode = consts.Error.NONE_ERROR;
					response.message= consts.Message.OK_MESSAGE;
					response.data = JSON.stringify(result);

					if(callback){
						callback(response);
					} 

				}

			});
		
	}
	
	

	return;

}



function processOrderUpdate(request,response,callback){

	var data =request.data;

	if(request.muuid===null || typeof(request.muuid)==='undefined'){

		response.errorCode=consts.Error.NEED_MERCHANT;
		response.message=consts.Message.NEED_MERCHANT;

		if(callback){
			callback(response);
		}
		return;
	}

	var param = null;

	
	if(data===null || typeof(data)==='undefined'){

		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;

		if(callback){
			callback(response);
		}

		return false;

	}else {
		console.log(data);
	}

	try {

	   param = JSON.parse(data);
	
	} catch (e) {
      console.log(e.message);
	}
	
	if(param===null){

		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;

		if(callback){

			callback(response);

		}

		return false;

	}
	
	if(param.attached===null || typeof(param.attached)==='undefined'){
		param.attached =null;
	}
	
	if(param.removes===null || typeof(param.removes)==='undefined'){
		param.removes =null;
	}

	
//	if((param.attached==null)&&param.removes==null ){
//
//		response.errorCode=consts.Error.ERROR_PARAM;
//		response.message=consts.Message.ERROR_PARAM+':attached or removes';
//
//		if(callback){
//			callback(response);
//		}
//		return;		
//	}
//	
	
	if(param.ID===null || typeof(param.ID)==='undefined' || param.ID===0  ){

		param.ID = null;
	}
	
	
	if(param.orderNO===null || typeof(param.orderNO)==='undefined' ){

		param.orderNO = null;
			
	}
	
	if(param.orderNO ===null && param.ID === null ){
		
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':ID or orderNO';
		if(callback){
			callback(response);
		}
		return;
		
	}

	 order.update(request.muuid,param,function(err,result){

		if(err){

			response.errorCode=consts.Error.ERROR_PARAM;
			response.message=consts.Message.ERROR_PARAM;

			if(callback){
				callback(response);
			}

		}else{

			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(result);

			if(callback){
				callback(response);
			} 

		}

	});

	return;

}




async function processOrderPlace(request,response,callback){

	var data =request.data;

	if(request.muuid===null || typeof(request.muuid)==='undefined'){

		response.errorCode=consts.Error.NEED_MERCHANT;
		response.message=consts.Message.NEED_MERCHANT;

		if(callback){
			callback(response);
		}
		return;
	}

	var param = null;

	if(data===null || typeof(data)==='undefined'){

		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;

		if(callback){
			callback(response);
		}

		return false;

	}else {
		console.log(data);
	}


	
	try {

	   param = JSON.parse(data);
	
	} catch (e) {
      console.log(e.message);
	}
	
	if(param===null){

		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;

		if(callback){

			callback(response);

		}

		return false;

	}
	
	param.token=request.token;

	
	if(param.attached===null || typeof(param.attached)==='undefined'){
		param.attached =null;
	}
	
	if(param.reserve!==null && typeof(param.reserve)!=='undefined'){
         param.reserve.reserveType =1;//预订
	}
	
	if(param.money===null || typeof(param.money)==='undefined'){
		param.money =null;
	}else{
		 if(param.money<0.0099){
			 param.money=null;
			
		 }else{
			 var money = Number(param.money);
			 param.money=Number(money.toFixed(2));
			 if(param.money <0.01) {param.money=null; }
		 }
	}
	
	if((param.attached===null)&&param.money===null ){

	//	response.errorCode=consts.Error.ERROR_PARAM;
	//	response.message=consts.Message.ERROR_PARAM+':attached or money';

//		if(callback){
//			callback(response);
//		}
//		return;
//		attached or money
		param.money=0;
	}
	

	
	 param.orderNO =  'd'+new Date().getTime()+'d';
	 
	 console.log('aaaaaafdf');
	 if(param.totalNum===null || typeof(param.totalNum)==='undefined'){
		 param.totalNum=0;
	 }
	 
	 if(param.deskNO===null || typeof(param.deskNO)==='undefined'){
		 param.deskNO='';
	 }

	 param.saleMoney=Number((Math.random()/2).toFixed(2));
	 if(param.saleMoney <0.02){param.saleMoney=0.02;}
	 if(param.money >=0.01 && param.money<10000000){
		 param.saleMoney=Number(Number(param.money).toFixed(2)); //(Math.random()/2).toFixed(2);
	 }
		
	 param.token=request.token;

	 if(typeof(param.type) === 'undefined' || (param.type !== 1 && param.type !==2)||
			 typeof(param.code) === 'undefined' || (param.code === null)){
		 order.insert(request.muuid,param,function(err,result){

				if(err){
					
					if(typeof(err.number)!== 'undefined' && err.number !== null) {
						response.errorCode = err.number;
						response.message = err.message;
						if(result!==null){
						   response.data = JSON.stringify(result);
						}

						if(callback){
							callback(response);
						}
						return;
						
					}

					response.errorCode=consts.Error.ERROR_PARAM;
					response.message=consts.Message.ERROR_PARAM;

					if(callback){

						callback(response);
					}

				}else{

					response.errorCode = consts.Error.NONE_ERROR;
					response.message= consts.Message.OK_MESSAGE;
					response.data = JSON.stringify(result);

					if(callback){
						callback(response);
					} 

				}

			});
		 return;

	}
	var code=null;
	var type =-1;
	var isMA = 0;

	if(param.code!==null || typeof(param.code)!=='undefined'){
		code = param.code;
	}
	
	if(param.isMA !==null && typeof(param.isMA)!=='undefined' && param.isMA ===1){
		isMA = 1;
	}
	
	if(param.type!==null && typeof(param.type)!=='undefined'&& (param.type === 1 || param.type ===2) ){
		type = param.type;
	}
	
	if(param.isMA !==null && typeof(param.isMA)!=='undefined' && param.isMA ===1){
		isMA = 1;
	}
	 console.log('processOrderPlace::ccccc');

	var conn =  await db.createDB();
	console.log('processOrderPlace::ccccc::dddddd');
	if(!conn){
		response.errorCode=consts.Error.ERROR_BIND_ERROR;
		response.message=consts.Message.ERROR_BIND_ERROR;
		if(callback){
			callback(response);
		}
		return false;
	}
	console.log('processOrderPlace::ccccc::eeeeee');
	var obj = await member.fetchAppIDandOpenID1(conn,request.muuid,code,type, null);
	
	if(!obj.openID){
        db.releaseDB(conn);
		var appid = null;
		var secret = null;
		var err = null;
		if(type===1){
			appid = obj.appid;
			secret = obj.appsecret;
			if(isMA===1){
				appid = obj.g_appid;
				secret = obj.g_appsecret;
			}
			try{
				obj.openID = await oauth2.loginWeixin(appid, secret,code,isMA, null);
			}catch(e){
				err = e;
			}
		}else if(type===2){
			appid = obj.alappid;
			secret = null;//obj.g_appsecret;
			try{
				obj.openID = await oauth2.loginZfb(appid, secret, code,null);
			}catch(e){
				err =e;
			}
		}
		if(err){
			response.errorCode=err.number;
			response.message=err.message;
			if(callback){
				callback(response);
			}
			return;
		}
		conn =  await db.createDB();
		console.log('processOrderPlace::ccccc::dddddd');
		if(!conn){
			response.errorCode=consts.Error.ERROR_BIND_ERROR;
			response.message=consts.Message.ERROR_BIND_ERROR;
			if(callback){
				callback(response);
			}
			return false;
		}
		var oo = await member.saveCodeTemp1(conn,code,obj.openID,type,null);//{
	
	}
    db.releaseDB(conn);
    param.openid=obj.openID;
    param.type=type;
    order.insert(request.muuid,param,function(err,result){
    	if(err){
    		if(typeof(err.number)!== 'undefined' && err.number !== null) {
				response.errorCode = err.number;
				response.message = err.message;
				if(result!==null){
				   response.data = JSON.stringify(result);
				}

				if(callback){
					callback(response);
				}
				return false;
						
    		}
    	
			response.errorCode=consts.Error.ERROR_PARAM;
			response.message=consts.Message.ERROR_PARAM;

			if(callback){

				callback(response);
			}

			
    	}else{

			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(result);

			if(callback){
				callback(response);
			} 
				
    	}
    });
	 
}



function processOrderDetails(request,response,callback){

	var data =request.data;

	if(request.muuid===null || typeof(request.muuid)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_MERCHANT;
		if(callback){
			callback(response);
		}
		return;
	}
	var muuid= request.muuid;
	var IDs=null;

	var param = null;

	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
	
	try {
	    param = JSON.parse(data);

	} catch (e) {
		
	
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	
	}
	
	if(param.IDs!==null && typeof(param.IDs)!=='undefined'){
		IDs=param.IDs;
	}
	
	if(IDs===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':IDs';
		if(callback){
			callback(response);
		}
		return;		
		
	}
		
	order.getDetails(IDs,muuid, function(err,objs){
		if(err){

			response.errorCode=consts.Error.ERROR_PARAM;
			response.message=consts.Message.ERROR_PARAM;

			if(callback){

				callback(response);
			}

		}else{

			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(objs);

			if(callback){
				callback(response);
			} 

		}

	});
	
}






function processOrderDetail(request,response,callback){

	var data =request.data;

	if(request.muuid===null || typeof(request.muuid)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_MERCHANT;
		if(callback){
			callback(response);
		}
		return;
	}
	
	var id=null;
    var  orderNO=null;
	var param = null;

	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
	
	try {
	    param = JSON.parse(data);

	} catch (e) {
		
	
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	
	}
	
	if(param.ID!==null && typeof(param.ID)!=='undefined'){
		id=param.ID;
	}
	
	if(param.orderNO!==null && typeof(param.orderNO)!=='undefined'){
		orderNO=param.orderNO;
	
	}
	
	if(id===null && orderNO===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':ID or OrderNO';
		if(callback){
			callback(response);
		}
		return;		
		
	}
	
	var needPrint = 0;

	if(param.needPrint!==null && typeof(param.needPrint)!=='undefined'){
		needPrint=param.needPrint;
	
	}
		
	order.getDetail(id,orderNO,request.muuid,needPrint, function(err,obj){
		if(err){

			response.errorCode=consts.Error.ERROR_PARAM;
			response.message=consts.Message.ERROR_PARAM;

			if(callback){

				callback(response);
			}

		}else{

			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(obj);

			if(callback){
				callback(response);
			} 

		}

	});
	
}



function processOrderDetailPrinter(request,response,callback){

	var data =request.data;

	if(request.muuid===null || typeof(request.muuid)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_MERCHANT;
		if(callback){
			callback(response);
		}
		return;
	}
	
	var printNO=null;
    var orderNO=null;
	var param = null;

	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
	
	try {
	    param = JSON.parse(data);

	} catch (e) {
		
	
	}


	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	
	}
	
	if(param.printNO!==null && typeof(param.printNO)!=='undefined'){
		printNO=param.printNO;
	}
	
	if(param.orderNO!==null && typeof(param.orderNO)!=='undefined'){
		orderNO=param.orderNO;
	}
	
	var needPrint =1;

	if(orderNO===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':OrderNO';
		if(callback){
			callback(response);
		}
		return;		
		
	}
	
	order.getPrintDetail(printNO,orderNO,request.muuid, function(err,obj){
		if(err){

			response.errorCode=consts.Error.ERROR_PARAM;
			response.message=consts.Message.ERROR_PARAM;

			if(callback){

				callback(response);
			}

		}else{

			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(obj);

			if(callback){
				callback(response);
			} 

		}

	});
	
}



function processTransferinDetail(request,response,callback){

	var data =request.data;
	  
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	var param = null;

	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
	
	try {
	    param = JSON.parse(data);

	} catch (e) {
		
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(param.gainedDate===null || typeof(param.gainedDate)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':gainedDate';
		if(callback){
			callback(response);
		}
		return;			
	}
	
	var result={};
	result.totalSum=0.0;
	result.totalTransfer=0;
	result.serviceFee=0.00;
	result.weixinRatio=0.25;
	result.aliRatio=0.38;
	result.qqRatio=0.35;
	result.gainedDate=param.gainedDate;

	console.log(Date.now());
	var fees =[4.90,3.00,5.00];
	result.details=[];
	for(var i=0;i<3;i++){
		var detail={};
		detail.sum=fees[i];
		detail.serviceFee=(fees[i]*0.25).toFixed(2);
		result.serviceFee+=detail.serviceFee;
		result.totalTransfer+=detail.sum;
		result.totalSum+=detail.sum;
		result.serviceFee=fees[i]*0.25;

//		               13011276195+
		detail.transferTime=param.gainedDate+(i*10001000);
	    detail.type=i%3+1;
		result.details.push(detail);
	}
	
	 result.count=result.details.length;
	 result.totalSum =parseFloat(result.totalSum.toFixed(2));
	 result.totalTransfer =parseFloat(result.totalTransfer.toFixed(2));
	 result.serviceFee =parseFloat(result.serviceFee.toFixed(2));

	 response.errorCode = consts.Error.NONE_ERROR;
	 response.message= consts.Message.OK_MESSAGE;
	 response.data = JSON.stringify(result);
	 if(callback){
		callback(response);
	} 
	
	
	return;

}

//processOrderListByBuyer

async function processBuyerOrderList(request,response,callback){

	var data =request.data;
	
	if(typeof(request.muuid)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':muuid';
		if(callback){
			callback(response);
		}
		return;			
	}
	
	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
	
	var param = null;
	try {
	    param = JSON.parse(data);

	} catch (e) {
		
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}
	
	var code=null;
	var type =-1;
	var openID = null;
	var isMA = 0;
	var detail =0;
	
	if(param.code!==null && typeof(param.code)!=='undefined'){
		code = param.code;
	}
	
	if(param.type!==null && typeof(param.type)!=='undefined' && (param.type === 1 || param.type ===2) ){
		type = param.type;
	}

	if(param.detail && param.detail ==1){
		detail = param.detail;
	}
	
	
	if(param.isMA !==null && typeof(param.isMA)!=='undefined' && param.isMA ===1){
		isMA = 1;
	}
	
	if(!((code !== null && (type === 1 || type ===2)))){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+'code 和 type';
		if(callback){
			callback(response);
		}
		return false;
	}
	console.log('aaaaa');
	
	var conn = await db.createDB();
	if(!conn){
		response.errorCode=consts.Error.ERROR_BIND_ERROR;
		response.message=consts.Message.ERROR_BIND_ERROR;
		if(callback){
			callback(response);
		}
		return false;
	}
		
	var obj = await member.fetchAppIDandOpenID1(conn,request.muuid,code,type, null);//function(obj){
		
	console.log('fetchAppIDandOpenID::'+ JSON.stringify(obj));
	if(!obj.openID){
		db.releaseDB(conn);

		var appid = null;
		var secret = null;
		var err = null;
		if( type===1){
			var appid = obj.appid;
			var secret = obj.appsecret;
			if(isMA===1){
				appid = obj.g_appid;
				secret = obj.g_appsecret;
			}
			try{
				obj.openID = await oauth2.loginWeixin(appid, secret,code,isMA,null);
			}catch(e){
				err =e;
			}
		}else if(type ===2){
			appid = obj.alappid;
			secret = null;//obj.g_appsecret;
			try{				
				obj.openID = await oauth2.loginZfb(appid, secret, code,null);
			}catch(e){
				err =e;
			}
		}
			
		if(err){
			response.errorCode=err.number;
			response.message=err.message;
			if(callback){
				callback(response);
			}
			return false;
		}
		
		conn = await db.createDB();
		if(!conn){
			response.errorCode=consts.Error.ERROR_BIND_ERROR;
			response.message=consts.Message.ERROR_BIND_ERROR;
			if(callback){
				callback(response);
			}
			return false;
		}
		var oo = await member.saveCodeTemp1(conn,code,obj.openID,type,null);
				
	}
	
	
	var err = null;
	var wrap = null;
	console.log("processBuyerOrderList::detail::111:"+ detail);
	
	try{
		wrap = await order.listBuyerOrder1(conn, obj.openID,request.muuid, type,detail, null);
	}catch(e){
		err = e;
	}
	 if(err){
		 response.errorCode=err.number;
		 response.message=err.message;
		 if(callback){
			callback(response);
		 }
		 
	 }else{
		 response.errorCode = consts.Error.NONE_ERROR;
		 response.message= consts.Message.OK_MESSAGE;
		 response.data=JSON.stringify(wrap);
		 if(callback){
			callback(response);
		} 
	 }
	
	 db.releaseDB(conn);
			
	return true;

}

//对Date的扩展，将 Date 转化为指定格式的String   
//月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，   
//年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)   
//例子：   
//(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423   
//(new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18   
Date.prototype.Format = function(fmt)   
{ //author: meizz   
var o = {   
 "M+" : this.getMonth()+1,                 //月份   
 "d+" : this.getDate(),                    //日   
 "h+" : this.getHours(),                   //小时   
 "m+" : this.getMinutes(),                 //分   
 "s+" : this.getSeconds(),                 //秒   
 "q+" : Math.floor((this.getMonth()+3)/3), //季度   
 "S"  : this.getMilliseconds()             //毫秒   
};   
if(/(y+)/.test(fmt))   
 fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));   
for(var k in o)   
 if(new RegExp("("+ k +")").test(fmt))   
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
return fmt;   
};


async function timerFunc(obj){
	obj.timeCount++;
	var d = new Date();
	var minutes = d.getMinutes();
	var timeStamp = new Date().format('yyyy-MM-dd hh:mm:ss');    
	
	
	console.log('timeCount:::'+ timeStamp+ '::' +obj.timeCount+'::'+ minutes +'::'+ d.getTime());
	order.adjustUpdateTime(function(err,obj){
		console.log('ok update_time_update');
	});
	var tt = true;
	try{
		await order.adjustUpdateTime(null);
	}catch(e){
		console.log('error::' + JSON.stringify(e));
		
		//console.log(e);
	}
	console.log('ss'+tt);
	
	setTimeout(timerFunc,120000,obj);
}
// start a timer to do some time schedule working
async function timeService(){
	
	
	var metor = {};
	metor.timeCount =1;
	setTimeout(timerFunc,1000,metor);
	
}
module.exports={
		processOrderPlace:processOrderPlace,
		processOrderDetail:processOrderDetail,
		processOrderDetails:processOrderDetails,
		processPayOrderList:processPayOrderList,
		processRefundDeclare:processRefundDeclare,
		processTransferinList:processTransferinList,
		processBuyerOrderList:processBuyerOrderList,
		processTransferinDetail:processTransferinDetail,
		processOrderSummary:processOrderSummary,
		processPaySummary:processPaySummary,
		processOrderAppoint:processOrderAppoint,
		processOrderUpdate:processOrderUpdate,
		processOrderUpdateDeskOrPush:processOrderUpdateDeskOrPush,
		processOrderMerge:processOrderMerge,
		processOrderUpdatePrintStatus:processOrderUpdatePrintStatus,
		processUserOrderList:processUserOrderList,

		processOrderDetailPrinter:processOrderDetailPrinter,
		processUserPayStatistics:processUserPayStatistics,
		processUserOrderStatistics:processUserOrderStatistics,
		processReserveList:processReserveList,
		processStatusQuery:processStatusQuery,
		processGiftList:processGiftList,

		processOrderEarningStatistics:processOrderEarningStatistics,
		processOrderEarningStatisticsReport:processOrderEarningStatisticsReport,
		processOrderPayStatisticsReport:processOrderPayStatisticsReport,
//		processOrderSalesStatisticsReport:processOrderSalesStatisticsReport,
		processCancel:processCancel,
		processCounterPay:processCounterPay,
		processUserOrderS500:processUserOrderS500,
		processUserOrderPayFlow:processUserOrderPayFlow,
		
      processUserOrderS501:processUserOrderS501,

//		processOrderListByBuyer:processOrderListByBuyer,
	
};

timeService();



